package com.zros.modules.system.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zros.common.base.domain.ZTree;
import com.zros.common.exception.JeecgBootException;
import com.zros.modules.system.entity.DataResource;
import com.zros.modules.system.mapper.DataResourceMapper;
import com.zros.modules.system.service.IDataResourceService;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author pangyinqiang
 * @since 2019-04-12
 */
@Service
public class DataResourceServiceImpl extends ServiceImpl<DataResourceMapper, DataResource>
		implements IDataResourceService {
	/**
	 * 异常日志记录对象
	 */
	private static final Logger logger = LoggerFactory.getLogger(DataResourceServiceImpl.class);

	@Autowired
	private DataResourceMapper dataResourceMapper;

	@Override
	public List<DataResource> queryDataResourceByUserId(Long userId,String dataResourceMethod) {
		HashMap<String,Object> map = new HashMap<String,Object>();
		map.put("userId", userId);
		map.put("dataResourceMethod", dataResourceMethod);
		List<DataResource> resourceList = dataResourceMapper.queryDataResourceByUserId(map);
		return resourceList;
	}

	@Override
	public List<DataResource> queryDataResourceByParentId(Long parentId) {
		List<DataResource> resourceList = dataResourceMapper.queryDataResourceTreeProc(parentId);
		List<DataResource> menus = new ArrayList<DataResource>();
		Map<Long, DataResource> resourceMap = new HashMap<Long, DataResource>();
		// 组装子父级目录
		for (DataResource resource : resourceList) {
			resourceMap.put(resource.getId(), resource);
		}
		for (DataResource resource : resourceList) {
			Long treePId = resource.getParentId();
			DataResource pTreev = resourceMap.get(treePId);
			if (null != pTreev && !resource.equals(pTreev)) {
				List<DataResource> nodes = pTreev.getChildren();
				if (null == nodes) {
					nodes = new ArrayList<DataResource>();
					pTreev.setChildren(nodes);
				}
				nodes.add(resource);
			} else {
				menus.add(resource);
			}
		}
		return menus;
	}

	@Override
	public List<ZTree> queryDataResourceTree(Long parentId, boolean assembleChildren) {
		List<ZTree> treeList = new ArrayList<ZTree>();
		try {
			List<DataResource> perList = dataResourceMapper.queryDataResourceTreeProc(parentId);
			for (DataResource per : perList) {
				ZTree tree = new ZTree();
				tree.setId(String.valueOf(per.getId()));
				tree.setpId(String.valueOf(per.getParentId()));
				tree.setName(per.getDataResourceName());
				tree.setOpen(true);
				tree.setExtKey(per.getDataResourceKey());
				tree.setExtLevel(String.valueOf(per.getDataResourceLevel()));
				tree.setExtType(per.getDataResourceType());
				tree.setDescription(per.getDescription());
				tree.setIsParent(per.getIsLeaf() == 0 ? true : false);
				treeList.add(tree);
			}
			// 是否组装子节点
			if (assembleChildren) {
				// 将children组装进去
				List<ZTree> menus = new ArrayList<ZTree>();
				Map<String, ZTree> zTreeMap = new HashMap<String, ZTree>();
				// 组装子父级目录
				for (ZTree tree : treeList) {
					zTreeMap.put(tree.getId(), tree);
				}
				for (ZTree tree : treeList) {
					String treePId = tree.getpId();
					ZTree pTreev = zTreeMap.get(treePId);
					if (null != pTreev && !tree.equals(pTreev)) {
						List<ZTree> nodes = pTreev.getChildren();
						if (null == nodes) {
							nodes = new ArrayList<ZTree>();
							pTreev.setChildren(nodes);
						}
						nodes.add(tree);
					} else {
						menus.add(tree);
					}
				}
				treeList = menus;
			}
		} catch (Exception e) {
			logger.error("权限树查询异常:", e);
			throw new JeecgBootException("权限树查询异常");
		}
		return treeList;
	}

	@Override
	public List<DataResource> selectDataResourceList(QueryWrapper<DataResource> wrapper) {
		List<DataResource> list = this.list(wrapper);
		return list;
	}

	@Override
	public boolean createDataResource(DataResource dataResource) {
		boolean result = this.save(dataResource);
		return result;
	}

	@Override
	public boolean updateDataResource(DataResource dataResource) {
		boolean result = this.updateById(dataResource);
		return result;
	}

	@Override
	public boolean deleteDataResource(Long dataResourceId) {
		QueryWrapper<DataResource> wpd = new QueryWrapper<DataResource>();
		wpd.and(e -> e.eq("id", dataResourceId).or().eq("parent_id", dataResourceId));
		boolean result = this.remove(wpd);
		return result;
	}

}
